package alingrad.engine.src.gameobjects;

import java.io.IOException;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import alingrad.engine.src.Resources;
import alingrad.engine.src.collisions.Collidable;
import alingrad.engine.src.collisions.CollisionMask;
import alingrad.engine.src.collisions.Collisions;
import alingrad.engine.src.core.RenderManager;
import alingrad.engine.src.graphics.Animation;
import alingrad.engine.src.graphics.AnimationHandler;
import alingrad.engine.src.graphics.Renderable;
import alingrad.engine.src.graphics.Sprite;

/**
 * A test game object.
 * @author alingrad
 *
 */

public class Rectangle extends GameObject implements Renderable, Collidable {

	public static final int GRAVITY_CONSTANT = 3000;

	//Drawing Code
	private Sprite sprite;
	private boolean isWalking;
	private boolean onGround;
	private float vspeed;
	private int direction;
	private float scale = 1f;
	private float rotation;
	private static final int ANIM_STAND = 0;
	private static final int ANIM_WALK = 1;
	private static final int ANIM_AIR = 2;

	private Rectangle other;

	private AnimationHandler anim = new AnimationHandler() {
		public void load() {
			sprites = new Sprite[21];
			animations = new Animation[3];
			int last = 0;
			int begin = 0;
			int end = begin + 8 - 1;
			int delay = 125;
			for (int i = begin; i <= end; i++) {
				String path = "sprites\\zero\\zero_stand";
				if (i + 1 < 10)
					path += "0";
				path += (i + 1) + ".png";
					sprites[i + last] = Resources.getSprite(path);
			}
			animations[ANIM_STAND] = new Animation(begin, end, delay);
			last = end + 1;
			begin = end + 1;
			end = begin + 10 - 1;
			delay = 100;
			for (int i = 0; i <= end - begin; i++) {
				String path = "sprites\\zero\\zero_run";
				if (i + 1 < 10)
					path += "0";
				path += (i + 1) + ".png";
					sprites[i + last] = Resources.getSprite(path);
			}
			animations[ANIM_WALK] = new Animation(begin, end, delay);
			last = end + 1;
			begin = end + 1;
			end = begin + 3 - 1;
			delay = 100;
			for (int i = 0; i <= end - begin; i++) {
				String path = "sprites\\zero\\zero_air";
				if (i + 1 < 10)
					path += "0";
				path += (i + 1) + ".png";
					sprites[i + last] = Resources.getSprite(path);
			}
			animations[ANIM_AIR] = new Animation(begin, end, delay);
			sprite = getCurrentSprite();
		}
	};

	/**
	 * Loads the textures for this class.
	 * @throws IOException
	 */
	public static void load() throws IOException {
		for (int i = 0; i < 8; i++) {
			String path = "sprites\\zero\\zero_stand";
			if (i + 1 < 10)
				path += "0";
			path += (i + 1) + ".png";
			Resources.loadTexture(path, true);
		}
		for (int i = 0; i < 10; i++) {
			String path = "sprites\\zero\\zero_run";
			if (i + 1 < 10)
				path += "0";
			path += (i + 1) + ".png";
			Resources.loadTexture(path, true);
		}
		for (int i = 0; i < 3; i++) {
			String path = "sprites\\zero\\zero_air";
			if (i + 1 < 10)
				path += "0";
			path += (i + 1) + ".png";
			Resources.loadTexture(path, true);
		}
	}

	public void setOther(Rectangle other) {
		this.other = other;
	}

	public Rectangle(int x, int y) {
		this.x = x;
		this.y = y;
		rotation = 0;
		anim.load();

		isWalking = false;
	}

	@Override
	public void initialize() {
		anim.resetAnimation(ANIM_STAND);
		onGround = false;
	}

	@Override
	public void update(long delta) {
		RenderManager.addToQueue(this, 0);
		isWalking = false;

		int xmovespeed = 150;

		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			if (onGround) {
				onGround = false;
				vspeed = -1000;
			}
		}

		scale = 1f;
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			scale = 2.5f;
		}

		if (!onGround) {
			vspeed += GRAVITY_CONSTANT * delta / 1000f;
		}
		else
			vspeed = 0;

		y += vspeed * delta / 1000f;
		if (y > 450) {
			onGround = true;
			y = 450;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
			x -= xmovespeed * delta / 1000f;
			isWalking = true;
			direction = -1;
		}else if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
			x += xmovespeed * delta / 1000f;
			isWalking = true;
			direction = 1;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
			rotation += 50 / 1000f;
		} else if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
			rotation -= 50 / 1000f;
		}
		if (onGround) {
			if (isWalking)
				anim.setAnimation(ANIM_WALK);
			else
				anim.setAnimation(ANIM_STAND);
		}else {
			anim.setAnimation(ANIM_AIR);
		}

		anim.update(delta);

		//Animation Stuff
		sprite = anim.getCurrentSprite();
		sprite.x = (int)x;
		sprite.y = (int)y;
		sprite.rotation = rotation;
		sprite.scale = scale;
		if (direction == 1)
			sprite.xmirror = false;
		else
			sprite.xmirror = true;
//		if (sprite.rotation > 90 && sprite.rotation < 270)
//			sprite.ymirror = true;
//		else
//			sprite.ymirror = false;

//		System.out.println("(" + Mouse.getX() + ", " + (RenderManager.HEIGHT - Mouse.getY()) + "). " + sprite.getCollisionMask());
		if (Collisions.checkCollision(this, Mouse.getX(), RenderManager.HEIGHT - Mouse.getY()))
			System.out.println("collides!");
		if (Collisions.checkCollision(this, other))
			System.out.println("COLLISION!");
		if (Mouse.isButtonDown(1))
			System.out.println("Mouse(" + Mouse.getX() + ", " + (RenderManager.HEIGHT - Mouse.getY()) + ")");

		if (x > 640) {
			System.out.println("Rectangle is no longer valid");
			isValid = false;
		}
	}

	public void destroy() {
	}

	@Override
	public void draw() {
		sprite.draw();

//		//saves matrix
//		GL11.glPushMatrix();
//
//		GL11.glTranslatef(x, y, 0);
//		GL11.glColor3f(1, 1, 1);
//
//		GL11.glPopMatrix();
	}

	@Override
	public CollisionMask getMask() {
		return sprite.getCollisionMask();
	}

}
